package Utils;

public class StringUtils {

    public static String InsertWord(String str, int i, String str1) throws Exception {
        if ((i > str.length()) || (i<0)) {
            throw new Exception("Ошибка: в строке нет символа с номером " + i + ".");
        }
        return str.substring(0, i) + str1 + str.substring(i);
    }

    public static int searchSubStr(String str1, String str2) {
        if ((str1.length()-str2.length()) < 0) return -1;
        for (int i=0; i<(str1.length()-str2.length()); i++) {
            String str3 = str1.substring(i, i+str2.length());
            if (str2.equals(str3)) return i;
        }
        return -1;
    }

    public static int getQuantityOfWords(String str) {
        int sost = 0, sum = 0;
        if ((str.length() != 0) && (str.charAt(0) != ' ')) sum++;
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') sost = 1;
            if (str.charAt(i) != ' ') {
                if (sost == 1) sum++;
                sost = 2;
            }
        }
        return sum;
    }

    public static int getQuantityOfCharWithSpaces(String str) {
        return str.length();
    }

    public static int getQuantityOfCharWithoutSpaces(String str) {
        int sumSpace = 0;
        for (int i=0; i<str.length(); i++)
            if (str.charAt(i) != ' ') sumSpace++;
        return sumSpace;
    }

    public static String getWordMax(String str) {
        int sost = 0, sum = 0, max = 0;
        String slovo = "", sl = "";
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') {
                if ((sost == 2) && (sum > max)) {
                    max = sum;
                    slovo = sl;
                }
                sost = 1;
            }
            if (str.charAt(i) != ' ') {
                if (sost == 1) {
                    sum = 0;
                    sl = "";
                }
                sl = sl + str.charAt(i);
                sum++;
                sost = 2;
            }
        }
        if (sum > max) {
            max = sum;
            slovo = sl;
        }
        return slovo;
    }

    public static String getWordMin(String str) {
        int sost = 0, sum = 0, min = 0;
        boolean f = true;
        String slovo = "", sl = "";
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') {
                if (sost == 2) {
                    if (f) {
                        min = sum;
                        slovo = sl;
                        f = false;
                    }
                    else if (sum < min) {
                        min = sum;
                        slovo = sl;
                    }
                }
                sost = 1;
            }
            if (str.charAt(i) != ' ') {
                if (sost == 1) {
                    sum = 0;
                    sl = "";
                }
                sl = sl + str.charAt(i);
                sum++;
                sost = 2;
            }
        }
        if (sum < min) {
            min = sum;
            slovo = sl;
        }
        return slovo;
    }

    //--------------------------------------------------

    public static StringBuffer InsertWord(StringBuffer str, int i, StringBuffer str1) throws Exception {
        if ((i > str.length()) || (i<0)) {
            throw new Exception("Ошибка: в строке нет символа с номером " + i + ".");
        }
        return new StringBuffer().append(str.substring(0,i)).append(str1).append(str.substring(i));
    }

    public static int searchSubStr(StringBuffer str1, StringBuffer str2) {
        if ((str1.length()-str2.length()) < 0) return -1;
        for (int i=0; i<(str1.length()-str2.length()); i++) {
            StringBuffer str3 = new StringBuffer(str1.substring(i, i+str2.length()));
            if (str2.equals(str3)) return i;
        }
        return -1;
    }

    public static int getQuantityOfWords(StringBuffer str) {
        int sost = 0, sum = 0;
        if ((str.length() != 0) && (str.charAt(0) != ' ')) sum++;
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') sost = 1;
            if (str.charAt(i) != ' ') {
                if (sost == 1) sum++;
                sost = 2;
            }
        }
        return sum;
    }

    public static int getQuantityOfCharWithSpaces(StringBuffer str) {
        return str.length();
    }

    public static int getQuantityOfCharWithoutSpaces(StringBuffer str) {
        int sumSpace = 0;
        for (int i=0; i<str.length(); i++)
            if (str.charAt(i) != ' ') sumSpace++;
        return sumSpace;
    }

    public static StringBuffer getWordMax(StringBuffer str) {
        int sost = 0, sum = 0, max = 0;
        StringBuffer slovo = new StringBuffer(""), sl = new StringBuffer("");
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') {
                if ((sost == 2) && (sum > max)) {
                    max = sum;
                    slovo = sl;
                }
                sost = 1;
            }
            if (str.charAt(i) != ' ') {
                if (sost == 1) {
                    sum = 0;
                    sl = new StringBuffer("");
                }
                sl = sl.append(str.charAt(i));
                sum++;
                sost = 2;
            }
        }
        if (sum > max) {
            max = sum;
            slovo = sl;
        }
        return slovo;
    }

    public static StringBuffer getWordMin(StringBuffer str) {
        int sost = 0, sum = 0, min = 0;
        boolean f = true;
        StringBuffer slovo = new StringBuffer(""), sl = new StringBuffer("");
        for (int i=0; i<str.length(); i++) {
            if (str.charAt(i) == ' ') {
                if (sost == 2) {
                    if (f) {
                        min = sum;
                        slovo = sl;
                        f = false;
                    }
                    else if (sum < min) {
                        min = sum;
                        slovo = sl;
                    }
                }
                sost = 1;
            }
            if (str.charAt(i) != ' ') {
                if (sost == 1) {
                    sum = 0;
                    sl = new StringBuffer("");
                }
                sl = sl.append(str.charAt(i));
                sum++;
                sost = 2;
            }
        }
        if (sum < min) {
            min = sum;
            slovo = sl;
        }
        return slovo;
    }
}